package com.zhangyx.crawler.core;

import com.zhangyx.crawler.comm.EncryptUtil;
import com.zhangyx.crawler.comm.FileUtil;
import com.zhangyx.crawler.comm.PropertiesUtil;
import com.zhangyx.crawler.comm.RedisUtil;
import org.apache.log4j.Logger;

import java.util.List;

public class SeedManager {
    private static Logger log = Logger.getLogger(SeedManager.class);

    public static Boolean loadSeedsToRedis(String seedName, String redisKey) {
        String filePathRoot = PropertiesUtil.get("seed.filePath");
        System.out.println(filePathRoot);
        try {
            if (RedisUtil.length(redisKey) > 0) {
                log.info("缓存中还有种子，停止添加");
                return true;
            }
            List<String> filesName = FileUtil.getFilesName(filePathRoot);
            for (String str : filesName) {
                if (str.contains(seedName)) {
                    List<String> urls = FileUtil.readFileByLines(filePathRoot, str);
                    for (String url : urls) {
                        RedisUtil.pushQueue(redisKey, url);
                    }
                    log.info(seedName + "加载了" + urls.size() + "个种子");
                    break;
                }
            }
        } catch (Exception e) {
            log.error("加载种子失败：" + seedName);
        }
        return true;
    }

    public static void removeSeeds(String redisKey) {
        RedisUtil.del(redisKey);
    }

    public static void addSeedToRedis(String redisKey, String url) {
        RedisUtil.pushQueue(redisKey, url);
    }

    public static boolean isRepeat(String value) {
        return RedisUtil.isExist(EncryptUtil.getMD5(value));
    }
    public static void addRepeat(String value) {
        RedisUtil.set(EncryptUtil.getMD5(value), "asd");
    }

    public static synchronized String getSeed(String redisKey) {
        return RedisUtil.popQueue(redisKey);
    }

    public static void main(String[] args){
        loadSeedsToRedis("kanzhun", "asd");
    }
}
